<?php

class Home extends CI_Controller {
	
	function Home()
	{
		parent::__construct();
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('titik_penjualan_model', '', TRUE);
		$this->load->model('Tamu_model', '', TRUE);
	}
	
	var $title = 'Home';
	
	function index()
	{
		//cek nih user udah login apa blom, kalo blom lempar ke halaman login
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		// Load default view
		$data['title'] = $this->title;
		$data['nama_user'] = 'Admin';
		$data['main_view'] = 'home_view';
		
		$all_titik_penjualan = $this->titik_penjualan_model->get_all_titik_penjualan();
		if($all_titik_penjualan == null)
			$data['all_titik_penjualan'] = null;
		else 
			$data['all_titik_penjualan'] = $all_titik_penjualan->result_array();
		$all_kordinat = $this->titik_penjualan_model->get_all_kordinat();
		$data['all_kordinat'] = $all_kordinat->result_array();
		
		$data['peternakan'] 		= $this->Tamu_model->get_titik_pasok("peternakan");
		$data['agen_sapi']			= $this->Tamu_model->get_titik_pasok("agen_sapi");
		$data['titik_penjualan'] 	= $this->Tamu_model->get_titik_pasok("titik_penjualan");
		$data['rph'] 				= $this->Tamu_model->get_titik_pasok("rph");
		$data['titik_pasok']		= $this->merge($data["peternakan"],$data["rph"],$data["titik_penjualan"],$data["agen_sapi"]);
		
		// Load view
		$this->load->view('template', $data);
		
		
	}
	
	function merge($peternakan,$rph,$titik_penjualan,$agen_sapi){
		$titik_pasok = array();
		if($peternakan){
			foreach($peternakan->result_array() as $data){
				$titik_pasok[] = array(
					"JENIS"		=> "1",
					"ID_TITIK"	=> $data["ID_TITIK_PASOK"],
					"NAMA"		=> $data["NAMA"],
					"ALAMAT"	=> $data["ALAMAT"],
					"TELP"		=> $data["NO_TELP"],
					"EMAIL"		=> $data["EMAIL"],
					"KODE_POS"	=> $data["KODE_POS"],
					"LAT"		=> $data["LAT"],
					"LON"		=> $data["LON"]
				);
			}
		}
		if($rph){
			foreach($rph->result_array() as $data){
				$titik_pasok[] = array(
					"JENIS"		=> "2",
					"ID_TITIK"	=> $data["ID_TITIK_PASOK"],
					"NAMA"		=> $data["NAMA"],
					"ALAMAT"	=> $data["ALAMAT"],
					"TELP"		=> $data["NO_TELP"],
					"EMAIL"		=> $data["EMAIL"],
					"KODE_POS"	=> $data["KODE_POS"],
					"LAT"		=> $data["LAT"],
					"LON"		=> $data["LON"]
				);
			}
		}
		if($titik_penjualan){
			foreach($titik_penjualan->result_array() as $data){
				$titik_pasok[] = array(
					"JENIS"		=> "3",
					"ID_TITIK"	=> $data["ID_TITIK_PASOK"],
					"NAMA"		=> $data["NAMA"],
					"ALAMAT"	=> $data["ALAMAT"],
					"TELP"		=> $data["NO_TELP"],
					"EMAIL"		=> $data["EMAIL"],
					"KODE_POS"	=> $data["KODE_POS"],
					"LAT"		=> $data["LAT"],
					"LON"		=> $data["LON"]
				);
			}
		}
		if($agen_sapi){
			foreach($agen_sapi->result_array() as $data){
				$titik_pasok[] = array(
					"JENIS"		=> "4",
					"ID_TITIK"	=> $data["ID_TITIK_PASOK"],
					"NAMA"		=> $data["NAMA"],
					"ALAMAT"	=> $data["ALAMAT"],
					"TELP"		=> $data["NO_TELP"],
					"EMAIL"		=> $data["EMAIL"],
					"KODE_POS"	=> $data["KODE_POS"],
					"LAT"		=> $data["LAT"],
					"LON"		=> $data["LON"]
				);
			}
		}
		
		return $titik_pasok;
	}
	
	
}

